<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');
require_once(dirname(__FILE__) . '/paybank.php');

need_login();

$total_money = abs(floatval($_POST['money']));
$action = strval($_POST['action']);

if (!$total_money && $action != 'redirect') {
	Session::Set('error', 'Valor adicionado a ser recarregado deve ser maior que 1');
	redirect( WEB_ROOT . '/credit/charge.php' );
}

$order_service = pay_getservice($_POST['paytype']);
$title = "{$login_user['email']}({$INI['system']['sitename']}recharge{$total_money})";
$now = time();
$randno = rand(1000,9999);

/* credit pay */
if ( $_POST['action'] == 'redirect' ) {
	redirect($_POST['reqUrl']);
}

/*******************/
/****** MOIP *******/
/*******************/

else if ( $order_service == 'moip' ) {

	$conta 		= $INI['moip']['acc'];
	$token   	= $INI['moip']['mid'];

	$item_id = "charge-{$login_user_id}-{$now}-{$randno}";
	$item_descricao = "RECARGA DE CREDITOS";
	$item_valor = $total_money;
	$item_quantity = 1;

	$post_url = "https://www.moip.com.br/PagamentoMoIP.do";
	$retorno 	= $INI['system']['wwwprefix']. '/order/moip/return.php';
	$return_url = $INI['system']['wwwprefix'] . '/order/moip/return.php';
	$notify_url = $INI['system']['wwwprefix'] . '/order/moip/notify.php';
	$cancel_url = $INI['system']['wwwprefix'] . "/meus_creditos";

	include template('order_charge');

}

/********************************/
/****** PAGAMENTO DIGITAL *******/
/********************************/

else if ( $order_service == 'pagamentodg' ) {

	$conta 		= $INI['pagamentodg']['acc'];
	$token   	= $INI['pagamentodg']['mid'];

	$item_id = "charge-{$login_user_id}-{$now}-{$randno}";
	$item_descricao = "RECARGA DE CREDITOS";
	$item_valor = $total_money;
	$item_quantity = 1;

	$post_url = "https://www.pagamentodigital.com.br/checkout/pay/";
	$retorno 	= $INI['system']['wwwprefix']. '/order/pagamentodg/return.php';
	$return_url = $INI['system']['wwwprefix'] . '/order/pagamentodg/return.php';
	$notify_url = $INI['system']['wwwprefix'] . '/order/pagamentodg/notify.php';
	$cancel_url = $INI['system']['wwwprefix'] . "/meus_creditos";

	include template('order_charge');

}

/* paypal support */
else if ( $order_service == 'paypal' ) {

	$cmd = '_xclick';
	$Business = $INI['paypal']['mid'];
	$location = $INI['paypal']['loc'];

	$item_number = "charge-{$login_user_id}-{$now}-{$randno}";
	$item_name = $title;
	$amount = $total_money;
	$quantity = 1;

	$post_url = "https://www.paypal.com/row/cgi-bin/webscr";

	$image_url = "";
	$return_url = $INI['system']['wwwprefix'] . "/meus_creditos";
	$notify_url = $INI['system']['wwwprefix'] . '/order/paypal/ipn.php';
	$cancel_url =   $INI['system']['wwwprefix'] . "/meus_creditos";

	include template('order_charge');
}
/************************/
/****** PAGSEGURO *******/
/************************/
else if ( $order_service == 'pagseguro' ) {

	$conta 		= $INI['pagseguro']['acc'];
	$token   	= $INI['pagseguro']['mid'];

	$item_id = "charge-{$login_user_id}-{$now}-{$randno}";
	$item_descricao = "RECARGA DE CREDITOS";
	$item_valor = $total_money;
	$item_quantity = 1;

	$post_url = "https://pagseguro.uol.com.br/checkout/checkout.jhtml";
	$retorno 	= $INI['system']['wwwprefix']. '/order/pagseguro/return.php';
	$return_url = $INI['system']['wwwprefix'] . '/order/pagseguro/return.php';
	$notify_url = $INI['system']['wwwprefix'] . '/order/pagseguro/notify.php';
	$cancel_url = $INI['system']['wwwprefix'] . "/meus_creditos";

	include template('order_charge');

}
elseif ( $order_service == 'bill' ) {

	$pname = urlencode($login_user['username']);
	$commodity_info =urlencode($title);
	$merchant_param = "";
	$pemail=$login_user['email'];
	$pid="";
	$merchantAcctId= $INI['bill']['mid'];
	$key=$INI['bill']['sec'];
	$inputCharset="1";
	$pageUrl=$INI['system']['wwwprefix'] . '/order/bill/return.php';
	$bgUrl=$INI['system']['wwwprefix'] . '/order/bill/return.php';

	$version="v2.0";
	$language="1";
	$signType="1";
	$payerName=$login_user['username'];
	$payerContactType="1";
	$payerContact=$login_user['email'];
	$orderId= "charge-{$login_user_id}-{$now}-{$randno}";
	$orderAmount=  intval($total_money * 100);
	$orderTime=date('YmdHis');
	$productName= preg_replace('/[\r\n\s]+/','',strip_tags($title));
	$productNum="1";
	$productId="";
	$productDesc="";
	$ext1="";
	$ext2="";
	$payType="00";
	$bankId="";
	$redoFlag="0";
	$pid="";
	$signMsgVal=appendParam($signMsgVal,"inputCharset",$inputCharset);
	$signMsgVal=appendParam($signMsgVal,"pageUrl",$pageUrl);
	$signMsgVal=appendParam($signMsgVal,"bgUrl",$bgUrl);
	$signMsgVal=appendParam($signMsgVal,"version",$version);
	$signMsgVal=appendParam($signMsgVal,"language",$language);
	$signMsgVal=appendParam($signMsgVal,"signType",$signType);
	$signMsgVal=appendParam($signMsgVal,"merchantAcctId",$merchantAcctId);
	$signMsgVal=appendParam($signMsgVal,"payerName",$payerName);
	$signMsgVal=appendParam($signMsgVal,"payerContactType",$payerContactType);
	$signMsgVal=appendParam($signMsgVal,"payerContact",$payerContact);
	$signMsgVal=appendParam($signMsgVal,"orderId",$orderId);
	$signMsgVal=appendParam($signMsgVal,"orderAmount",$orderAmount);
	$signMsgVal=appendParam($signMsgVal,"orderTime",$orderTime);
	$signMsgVal=appendParam($signMsgVal,"productName",$productName);
	$signMsgVal=appendParam($signMsgVal,"productNum",$productNum);
	$signMsgVal=appendParam($signMsgVal,"productId",$productId);
	$signMsgVal=appendParam($signMsgVal,"productDesc",$productDesc);
	$signMsgVal=appendParam($signMsgVal,"ext1",$ext1);
	$signMsgVal=appendParam($signMsgVal,"ext2",$ext2);
	$signMsgVal=appendParam($signMsgVal,"payType",$payType);
	$signMsgVal=appendParam($signMsgVal,"bankId",$bankId);
	$signMsgVal=appendParam($signMsgVal,"redoFlag",$redoFlag);
	$signMsgVal=appendParam($signMsgVal,"pid",$pid);
	$signMsgVal=appendParam($signMsgVal,"key",$key);
	$signMsg= strtoupper(md5($signMsgVal));

	include template('order_charge');
}
else if ( $order_service == 'alipay' ) {

	$_input_charset = 'utf-8';
	$service = 'create_direct_pay_by_user';
	$partner = $INI['alipay']['mid'];
	$security_code = $INI['alipay']['sec'];
	$seller_email = $INI['alipay']['acc'];

	$sign_type = 'MD5';
	$out_trade_no = "charge-{$login_user_id}-{$now}-{$randno}";

	$return_url = $INI['system']['wwwprefix'] . '/order/alipay/return.php';
	$notify_url = $INI['system']['wwwprefix'] . '/order/alipay/notify.php';
	$show_url =   $INI['system']['wwwprefix'] . "/meus_creditos";

	$subject = $title;
	$body = $show_url;
	$quantity = 1;

	$parameter = array(
			"service"         => $service,
			"partner"         => $partner,
			"return_url"      => $return_url,
			"notify_url"      => $notify_url,
			"_input_charset"  => $_input_charset,
			"subject"         => $subject,
			"body"            => $body,
			"out_trade_no"    => $out_trade_no,
			"total_fee"       => $total_money,
			"payment_type"    => "1",
			"show_url"        => $show_url,
			"seller_email"    => $seller_email,
			);
	$alipay = new AlipayService($parameter, $security_code, $sign_type);
	$sign = $alipay->Get_Sign();
	include template('order_charge');
}
else {
	redirect( WEB_ROOT. "/meus_creditos");
}

Function appendParam($returnStr,$paramId,$paramValue){
	if($returnStr!=""){
		if($paramValue!=""){
			$returnStr.="&".$paramId."=".$paramValue;
		}
	}else{
		If($paramValue!=""){
			$returnStr=$paramId."=".$paramValue;
		}
	}
	return $returnStr;
}